package com.algorithm;

/**
 * 相交链表
 */
public class IntersectionOfTwoLinkedLists {


    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode n1=headA;
        ListNode n2=headB;
        while (n1!=n2){
            if (n1==null){
                n1=headB;
            }else {
                n1=n1.next;
            }
            if (n2==null){
                n2=headA;
            }else {
                n2=n2.next;
            }
        }
        return n1;
    }

    public static void main(String[] args) {
        ListNode listNode1=new ListNode(0,new ListNode(9,new ListNode(1,new ListNode(2,new ListNode(4)))));
        ListNode listNode2=new ListNode(3,new ListNode(2,new ListNode(4)));
        ListNode node = getIntersectionNode(listNode1, listNode2);
        System.out.println(node);

    }
}
